package com.tadpole.music.presenter.me.yun;

import com.tadpole.music.bean.me.yun.YunStudentInfoBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.yun.YunStudentInfoIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 蝌蚪云考试 获取考生详情
 * Created By 孙岩
 * on 2021/2/9
 */
public class YunStudentInfoPresenter extends BasePresenter<YunStudentInfoIView> {
    @SuppressWarnings("unchecked")
    public void getStudentInfo() {
        if (isViewAttached()) {
            String url = UrlConfig.getYunStudentInfo;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.YUN_TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONObject jsonObject = new JSONObject(object.getString("exam"));
                                    YunStudentInfoBean.DataBean.ExamBean examBean = new YunStudentInfoBean.DataBean.ExamBean(
                                            JsonParseUtil.getInt(jsonObject, "id"),
                                            JsonParseUtil.getStr(jsonObject, "title"),
                                            JsonParseUtil.getStr(jsonObject, "type"),
                                            JsonParseUtil.getStr(jsonObject, "works_type"),
                                            JsonParseUtil.getStr(jsonObject, "content")
                                    );

                                    YunStudentInfoBean.DataBean bean = new YunStudentInfoBean.DataBean(
                                            JsonParseUtil.getInt(object, "id"),
                                            JsonParseUtil.getStr(object, "exam_no"),
                                            JsonParseUtil.getStr(object, "idcode"),
                                            JsonParseUtil.getStr(object, "name"),
                                            JsonParseUtil.getBoolean(object, "is_test"),
                                            JsonParseUtil.getStr(object, "exam_address"),
                                            examBean
                                    );
                                    getView().showInfo(bean);
                                } else {
                                    ToastUtils.show(obj.getString("Msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
